﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using Macds.Core.XML;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Macds.Test.Core.XML
{
    [TestClass]
    public class XMLHelperTest
    {
        [TestMethod]
        public void GetSchemaTest()
        {
            string xml = @"<Database>
                            <companyID>105</companyID>
                            <companyName>prueba11195</companyName>
                            <companyDescription>prueba11195</companyDescription>
                            <rowCreated>8/17/2015 5:00:31 PM</rowCreated>
                            <rowModified>8/19/2015 2:57:08 PM</rowModified>
                            </Database>";

            var reader = XElement.Parse(xml).CreateReader();
            var schemaSet = new XmlSchemaInference().InferSchema(reader);

            var documents = new List<XDocument>();
            foreach (XmlSchema schema in schemaSet.Schemas())
            {
                var doc = new XDocument();
                using (XmlWriter xw = doc.CreateWriter())
                {
                    schema.Write(xw);
                }

                documents.Add(doc);
            }

            var xmlStr = documents.Count > 0 ? documents[0].ToString() : "";
        }

        [TestMethod]
        public void GetObjectFomSchemaTest()
        {
            string xml = @"<Database>
                            <companyID>105</companyID>
                            <companyName>prueba11195</companyName>
                            <companyDescription>prueba11195</companyDescription>
                            <rowCreated>8/17/2015 5:00:31 PM</rowCreated>
                            <rowModified>8/19/2015 2:57:08 PM</rowModified>
                            <Customer active='true'>
                                <FirstName>Marco</FirstName>
                                <LastName>Castillo</LastName>
                            </Customer>
                            <Required optional='true' alpha='ct56'></Required>
                            <Setting>1</Setting>
                            <Setting>2</Setting>
                            <Custom optional='true'><Code>1</Code><Data>Cent</Data></Custom>
                            <Custom optional='true'><Code>2</Code><Data>Money</Data></Custom>
                            <Global><Element active='true'><Name>Name1</Name><Amount>1</Amount></Element><Element><Name>Name2</Name><Amount>2</Amount></Element></Global>
                            <Global><Element><Name>Name3</Name><Amount>3</Amount></Element><Element><Name>Name4</Name><Amount>4</Amount></Element></Global>
                            </Database>";

            var manager = new XMLSchemaClassBuilderManager(xml);
            manager.IsXMLString = true;
            var classes = manager.GetClassesAtOnce();
        }

        [TestMethod]
        public void GetObjectFomTest()
        {
            string xml = @"<Database>
                            <companyID>105</companyID>
                            <companyName>prueba11195</companyName>
                            <companyDescription>prueba11195</companyDescription>
                            <rowCreated>8/17/2015 5:00:31 PM</rowCreated>
                            <rowModified>8/19/2015 2:57:08 PM</rowModified>
                            <Customer active='true'>
                                <FirstName>Marco</FirstName>
                                <LastName>Castillo</LastName>
                            </Customer>
                            <Required optional='true' alpha='ct56'></Required>
                            <Setting>1</Setting>
                            <Setting>2</Setting>
                            <Custom optional='true'><Code>1</Code><Data>Cent</Data></Custom>
                            <Custom optional='true'><Code>2</Code><Data>Money</Data></Custom>
                            <Global><Element active='true'><Name>Name1</Name><Amount>1</Amount></Element><Element><Name>Name2</Name><Amount>2</Amount></Element></Global>
                            <Global><Element><Name>Name3</Name><Amount>3</Amount></Element><Element><Name>Name4</Name><Amount>4</Amount></Element></Global>
                            </Database>";

            var manager = new XmlClassBuilderManager(xml);
            manager.IsXMLString = true;
            var classes = manager.GetClassesAtOnce();
        }

        [TestMethod]
        public void GetObjectWithDatabaseSchemaTest()
        {
            string xml = @"<Database>
                            <companyID>105</companyID>
                            <companyName>prueba11195</companyName>
                            <companyDescription>prueba11195</companyDescription>
                            <rowCreated>8/17/2015 5:00:31 PM</rowCreated>
                            <rowModified>8/19/2015 2:57:08 PM</rowModified>
                            <Customer active='true'>
                                <FirstName>Marco</FirstName>
                                <LastName>Castillo</LastName>
                            </Customer>
                            <Required optional='true' alpha='ct56'></Required>
                            <Setting>1</Setting>
                            <Setting>2</Setting>
                            <Custom optional='true'><Code>1</Code><Data>Cent</Data></Custom>
                            <Custom optional='true'><Code>2</Code><Data>Money</Data></Custom>
                            <Global><Element active='true'><Name>Name1</Name><Amount>1</Amount></Element><Element><Name>Name2</Name><Amount>2</Amount></Element></Global>
                            <Global><Element><Name>Name3</Name><Amount>3</Amount></Element><Element><Name>Name4</Name><Amount>4</Amount></Element></Global>
                            </Database>";
            var helper = new XMLHelper();

            dynamic obj = helper.GetObject(xml, true);
        }

        [TestMethod]
        public void GetJSONTest()
        {
            string xmlUrl = "https://sports.yahoo.com/nfl/teams/mia/rss.xml";
            var helper = new XMLHelper();

            string json = helper.GetJSON(xmlUrl);
        }

        [TestMethod]
        public void GetObjectTest()
        {
            string xmlUrl = "https://sports.yahoo.com/nfl/teams/mia/rss.xml";
            var helper = new XMLHelper();

            dynamic obj = helper.GetObject(xmlUrl);
            Assert.IsNotNull(obj);
            Assert.IsNotNull(obj.channel);
            Assert.IsTrue(obj.channel.description == "Latest news and information about the Miami Dolphins.");
        }

        [TestMethod]
        public void GetObjectFromXMLStringTest()
        {
            string xml = "<Customer><Name>Marco</Name><LastName>Castillo</LastName><DateOfBirth>6/25/2015 12:00:40 PM</DateOfBirth><Security><Password>123456</Password><Token>a1234</Token></Security></Customer>";
            var helper = new XMLHelper();

            dynamic obj = helper.GetObject(xml, true);
        }

        [TestMethod]
        public void GetListOFObjectTest()
        {
            string xml = @"<Database><Rows>
                            <Row>
                            <companyID>1</companyID>
                            <companyName>prueba11195</companyName>
                            <companyDescription>prueba11195</companyDescription>
                            <rowCreated>8/17/2015 5:00:31 PM</rowCreated>
                            <rowModified>8/19/2015 2:57:08 PM</rowModified>
                            </Row>
                            <Row>
                            <companyID>2</companyID>
                            <companyName>DEMO2</companyName>
                            <companyDescription>Empresa DEMO 2</companyDescription>
                            <rowCreated>8/17/2015 5:01:05 PM</rowCreated>
                            <rowModified>8/19/2015 2:57:18 PM</rowModified>
                            </Row></Rows></Database>";

            var helper = new XMLHelper();

            dynamic obj = helper.GetObject(xml, true);
        }

        [TestMethod]
        public void LargeXmlFileTest()
        {
            string xmlUrl = "http://localhost/large.xml";
            var helper = new XMLHelper();

            dynamic obj = helper.GetObject(xmlUrl);
        }
    }
}
